package zlx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import zlx.pojo.User;
import zlx.service.UserService;

import javax.annotation.Resource;
import java.util.HashMap;

@Controller
public class UserController {
    @Resource(name = "userService")

    private UserService userService;
    //去登陆页面
    @RequestMapping(value = "/user/toLogin",method = RequestMethod.GET)
    public String toLogin(){
        return "/pages/user/login.jsp";
    }

    //登录成功
    @RequestMapping(value = "/user/loginSuccess",method = RequestMethod.GET)
    public String loginSuccess(){
        return "/pages/user/login_success.jsp";
    }

    //验证登录
    @RequestMapping(value = "/user/login",method =RequestMethod.GET)
    public String login(User user, Model model){
        try {
            User loginInfo = userService.CheckLoginInfo(user);
        } catch (Exception e) {
            model.addAttribute("error","用户名或密码错误！");
            return "/user/toLogin";
        }
        return "redirect:/user/loginSuccess";
    }
//    用户注册
    @RequestMapping(value = "/user/regist",method = RequestMethod.POST)
    public String userRegist(User user){
        //引入插入用户数据对象
        userService.userRegist(user);
        return "/pages/user/regist_success.jsp";
    }

    //Ajax异步校验注册用户名
    @RequestMapping(value = "/user/checkUsername",produces="text/html;charset=UTF-8")
    @ResponseBody
    public String checkUsername(@RequestBody String username){
        User user = userService.checkUserByUsername(username);
        if (user!=null){
            System.out.println(""+user);
            return "用户已存在，请修改你的名字！";
        }
        return null;
    }

}
